package com.lyy.aspect;

import com.lyy.entity.Dog;
import lombok.SneakyThrows;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.util.Arrays;

/**
 * @author Liangyaoyun
 */
public class myAspect {
    public void insert() {
        System.out.println("前置通知：鉴权");
    }

    public void afterinsert() {
        System.out.println("后置通知");
    }

    public void methon02(JoinPoint joinPoint, Exception ex) {
        throw new RuntimeException("methon02爆发异常");
    }


    /**
     * 返回通知方法
     */
    public void afterReturnAdvice(JoinPoint joinPoint, Dog dog) {
        dog.setDogName("小白");
        System.out.println("返回通知: 修改返回值的dogName属性");
    }

    @SneakyThrows
    public Object aroundAdvice(ProceedingJoinPoint pjp) {
        // 业务方法返回值
        Object result = null;
        try {
            Object[] args = pjp.getArgs();
            // AOP前置通知
            System.out.println("AOP前置通知");
            System.out.println("AOP前置通知：获取参数" + Arrays.toString(args));
            // 代理执行: 反射并执行业务方法，其返回值就是业务方法的原返回值
            result = pjp.proceed(pjp.getArgs());
            // AOP后置通知
            System.out.println("AOP后置通知");
            System.out.println("AOP返回后通知");
        } catch (Exception d) {
            System.out.println("AOP异常通知");
        }
        return result;
    }

}
